package com.ruoyi.web.controller.zhuangxiucontroller.orderformController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.WarehouseInfo;
import com.ruoyi.system.domain.zhuangxiumain.provinces.City;
import com.ruoyi.system.domain.zhuangxiumain.provinces.Province;
import com.ruoyi.system.domain.zhuangxiumain.provinces.Town;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.BrandInfo;
import com.ruoyi.system.service.zhaungxiuservice.provincesService.ProvinceService;
import com.ruoyi.system.service.zhaungxiuservice.shopService.WarehouseInfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/store/warehouse")
public class WarehouseInfoController extends BaseController {

    @Autowired
    private WarehouseInfoService warehouseInfoService;

    @Autowired
    private TokenService tokenService;

    @Autowired(required = false)
    private ProvinceService provinceService;
    /**
     * 查询仓库信息列表
     */
    @PreAuthorize("@ss.hasPermi('store:warehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarehouseInfo warehouseInfo)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        warehouseInfo.setUserId(userId);
        List<WarehouseInfo> list = warehouseInfoService.selectWarehouseInfoList(warehouseInfo);
        return getDataTable(list);
    }

    /**
     * 导出仓库信息列表
     */
    @PreAuthorize("@ss.hasPermi('store:warehouse:export')")
    @Log(title = "仓库信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WarehouseInfo warehouseInfo)
    {
        List<WarehouseInfo> list = warehouseInfoService.selectWarehouseInfoList(warehouseInfo);
        ExcelUtil<WarehouseInfo> util = new ExcelUtil<WarehouseInfo>(WarehouseInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 获取仓库信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:warehouse:query')")
    @GetMapping(value = "/{wid}")
    public AjaxResult getInfo(@PathVariable("wid") Integer wid)
    {
        return AjaxResult.success(warehouseInfoService.selectWarehouseInfoById(wid));
    }

    /**
     * 新增仓库信息
     */
    @PreAuthorize("@ss.hasPermi('store:warehouse:add')")
    @Log(title = "仓库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseInfo warehouseInfo){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        warehouseInfo.setCustomerId(userId);

        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        warehouseInfo.setWarehouseSn(num);

        warehouseInfo.setModifiedTime(new Date());
        return toAjax(warehouseInfoService.insertWarehouseInfo(warehouseInfo));
    }

    /**
     * 修改仓库信息
     */
    @PreAuthorize("@ss.hasPermi('store:warehouse:edit')")
    @Log(title = "仓库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarehouseInfo warehouseInfo)
    {
        return toAjax(warehouseInfoService.updateWarehouseInfo(warehouseInfo));
    }

    /**
     * 删除仓库信息
     */
    @PreAuthorize("@ss.hasPermi('store:warehouse:remove')")
    @Log(title = "仓库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wids}")
    public AjaxResult remove(@PathVariable Integer[] wids)
    {
        return toAjax(warehouseInfoService.deleteWarehouseInfoByIds(wids));
    }




    @PreAuthorize("@ss.hasPermi('store:warehouse:province')")
    @GetMapping(value ="/province")
    public List<Province> findAllProvince(){
        return provinceService.findAllProvince();
    }


    @PreAuthorize("@ss.hasPermi('store:warehouse:city')")
    @GetMapping(value ="/city/{provinceCode}")
    public List<City> findAllCity(@PathVariable("provinceCode") String provinceCode){
        return provinceService.findAllCity(provinceCode);
    }

    @PreAuthorize("@ss.hasPermi('store:warehouse:town')")
    @GetMapping(value ="/town/{cityCode}")
    public List<Town> findAllTown(@PathVariable("cityCode") String cityCode){
        return provinceService.findAllTown(cityCode);
    }




}
